package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.WarehouseMaterialMapper;
import com.indusfo.spc.pojo.WarehouseMaterial;
import com.indusfo.spc.service.WarehouseMaterialService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class WarehouseMaterialServiceImpl implements WarehouseMaterialService {

    private  static  final Logger logger = LoggerFactory.getLogger(WarehouseMaterialServiceImpl.class);

    @Autowired
    private WarehouseMaterialMapper warehouseMaterialMapper;

    @Override
    public JSONObject selectWarehouseMaterial(WarehouseMaterial warehouseMaterial) {
        List<WarehouseMaterial> warehouseMaterialList = null;
        int count = 0;
        try {
            Integer pagesize = warehouseMaterial.getPagesize();
            Integer pageindex = warehouseMaterial.getPageindex();
            if(pagesize != null && pageindex != null) {
                warehouseMaterial.setIncept(pagesize*(pageindex - 1));
            }
            if(warehouseMaterial.getWarehousePlaceId() == null) {
                throw new ParamsErrorException("请选择库位");
            }
            warehouseMaterialList = warehouseMaterialMapper.selectWarehouseMaterial(warehouseMaterial);
            //查询分页总记录数
            count = warehouseMaterialMapper.countWarehouseMaterial(warehouseMaterial); //返回查询到的总记录数
            if (warehouseMaterialList.isEmpty()) {
                return JSONObject.oK("没有查询到相关数据", warehouseMaterialList, count);
            }
            return JSONObject.oK("查询成功", warehouseMaterialList, count);
        } catch (
                GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject insertWarehouseMaterial(WarehouseMaterial warehouseMaterial) {
        try {
            if(warehouseMaterial.getWarehouseMaterialId()!= null) {
                throw new ParamsErrorException("新增时，不能填写id");
            }
            checkParam(warehouseMaterial);
            // 调用存储过程
            int row = warehouseMaterialMapper.insertSelective(warehouseMaterial);
            if(row == 0) {
                throw new ModifyFailedException("数据新增失败");
            }
            return JSONObject.oK("新增成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject updateWarehouseMaterial(WarehouseMaterial warehouseMaterial) {
        try {
            if(warehouseMaterial.getWarehouseMaterialId()== null) {
                throw new ParamsErrorException("请选择要更新的");
            }
            checkParam(warehouseMaterial);
            // 调用存储过程
            int row = warehouseMaterialMapper.updateByPrimaryKeySelective(warehouseMaterial);
            if(row == 0) {
                throw new ModifyFailedException("数据更新失败！");
            }
            return JSONObject.oK("更新成功！");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject definesWarehouseMaterial(Long[] warehouseMaterialIds, Integer dataState) {
        try {
            if(dataState == null) {
                throw new ParamsErrorException("数据状态不能为空");
            }else if(dataState != 1 && dataState != 2 && dataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if (warehouseMaterialIds == null) {
                throw new ParamsErrorException("id不能为空");
            }
            // 执行存储过程
            int row = warehouseMaterialMapper.definesWarehouseMaterial(warehouseMaterialIds, dataState);
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (dataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                }
            } else {
                switch (dataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                }
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 参数校验
     */
    private void checkParam(WarehouseMaterial warehouseMaterial) {
        if (warehouseMaterial.getWarehousePlaceId() == null) {
            throw new ParamsErrorException("请选择库位");
        }
        if (warehouseMaterial.getlMatId() == null) {
            throw new ParamsErrorException("请选择物料");
        }
        if (StringUtils.isEmpty(warehouseMaterial.getWarehouseMaterialNum())) {
            throw new ParamsErrorException("请填写数量");
        }
        if (warehouseMaterial.getWarehouseMaterialNum().getBytes().length > 50) {
            throw new ParamsErrorException("库位编码超过规定,长度50");
        }
        // 库位 物料
        Integer depCounts = warehouseMaterialMapper.queryWarehouseMaterial(warehouseMaterial);
        if (depCounts !=0) {
            throw new ParamsErrorException("该物料已存在");
        }
    }

}
